package com.hzdl.house_mgr.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.house_mgr.bean.Admin;
import com.hzdl.house_mgr.bean.Menu;
import com.hzdl.house_mgr.service.MenuService;
import com.hzdl.house_mgr.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("sysMenu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @RequestMapping("indexLeftMenu")
    public Object indexLeftMenu(Integer id, HttpSession session){

        //获取登陆对象
        Object session_admin = session.getAttribute("session_admin");
        if(session_admin == null){
            return null;
        }
        Admin admin = (Admin) session_admin;
        List<Menu> all = menuService.findById(id);
        Map<String, Object> map = new HashMap<>();
        //超级管理员
        if(admin.getType() == 1){
            if(id == null){
                id =1;
            }
            List<Menu> list = menuService.findById(id);
            map.put("contentManagement",list);
        }else{
            //普通人员：需要查询对应的菜单
            List<Menu> menus = menuService.findByUserId(admin.getAdminId());  //没有层级关系
            //把menu转换成有层级关系的数据
            List<Menu> list = new ArrayList<>();
            for(Menu menu:menus){
                if(menu.getPid()!=null && menu.getPid() == 1){
                    list.add(menu);
                    continue;
                }
                for(Menu menu1:list){
                    if(menu.getPid() == menu1.getId()){
                        menu1.getChildren().add(menu);
                    }
                }
            }
            map.put("contentManagement",list);
        }
        return map;
    }

    @RequestMapping("menu/menuLeftJson")
    public AjaxResult menuLeftJson(){
        List<Menu> all = menuService.findById(0);
        return AjaxResult.ok(all);
    }

    @RequestMapping("menu/menuRightJson")
    public AjaxResult menuRightJson(String name,Integer page,Integer limit,Integer id){
        PageInfo pageInfo = menuService.findByWhere(name, page, limit,id);
        Integer count = menuService.getCountByWhere(name,id);
        return AjaxResult.ok(count,pageInfo.getList());
    }

    @RequestMapping("menu/menuAddOrUpdate")
    public AjaxResult menuRightJson(Menu menu){

        if(menu.getId() == null){
            int i = menuService.addMenu(menu);
            if(i>0){
                return AjaxResult.ADD_SUCCESS();
            }else{
                return AjaxResult.ADD_FAILED();
            }
        }else{
            int i = menuService.updateMenu(menu);
            if(i>0){
                return AjaxResult.UPD_SUCCESS();
            }else{
                return AjaxResult.UPD_FAILED();
            }
        }

    }

    @RequestMapping("menu/menuDelete")
    public AjaxResult menuDelete(Integer id){

        int count = menuService.findByPid(id);

        if(count!=0){
            return AjaxResult.DEL_HAS_CH();
        }else{
            int i = menuService.delMenu(id);
            if(i>0){
                return AjaxResult.DEL_SUCCESS();
            }else{
                return AjaxResult.DEL_FAILED();
            }
        }
    }
}
